package JFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import Class.*;

// 查询功能界面
public class QueryJFrame extends JFrame implements ActionListener {
    JButton btnQueryBalance = new JButton("查询账户余额");
    JButton btnDemandRecords = new JButton("活期存款记录");
    JButton btnTimeRecords = new JButton("定期存款记录");
    JButton btnTransferRecords = new JButton("查看转账记录");
    JButton btnWithdrawalRecords = new JButton("查看取款记录");

    // 查询功能界面设置
    public QueryJFrame() {
        this.setName("村镇银行业务储蓄系统");
        this.setTitle("用户操作界面");
        this.setSize(240, 400);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(2);
        this.setLayout(new FlowLayout());
        this.setVisible(true);

        JPanel p1 = new JPanel();
        p1.add(btnQueryBalance);
        this.add(p1);
        JPanel p2 = new JPanel();
        p2.add(btnDemandRecords);
        this.add(p2);
        JPanel p3 = new JPanel();
        p3.add(btnTimeRecords);
        this.add(p3);
        JPanel p4 = new JPanel();
        p4.add(btnTransferRecords);
        this.add(p4);
        JPanel p5 = new JPanel();
        p5.add(btnWithdrawalRecords);
        this.add(p5);

        btnQueryBalance.addActionListener(this);
        btnDemandRecords.addActionListener(this);
        btnTimeRecords.addActionListener(this);
        btnWithdrawalRecords.addActionListener(this);
        btnTransferRecords.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnQueryBalance) {
            BalanceQueryJFrame balanceQueryJFrame = new BalanceQueryJFrame();
            balanceQueryJFrame.setVisible(true);
        }
        if(e.getSource() == btnDemandRecords) {
            try {
                queryDemandRecords(new DemandRecordsJFrame());
            }catch (Exception exception){
            };
        }
        if(e.getSource() == btnTimeRecords) {
            try {
                queryTimeRecords(new TimeRecordsJFrame());
            }catch (Exception exception){
            };
        }
        if(e.getSource() == btnWithdrawalRecords) {
            try {
                queryWithdrawalRecords(new WithdrawalRecordsJFrame());
            }catch (Exception exception){
            };
        }
        if(e.getSource() == btnTransferRecords){
            try{
                queryTransferRecords(new TransferRecordsJFrame());
            } catch (Exception exception){
            };
        }
    }

    public void queryDemandRecords(DemandRecordsJFrame jFrame) throws Exception{
        User user = LoginJFrame.getUser();
        Statement statement = null;
        ResultSet result = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String ur1 = "jdbc:mysql://localhost:3306/bank";
            String username = "root";               //数据库用户名
            String mypassword = "20222746";          //数据库密码
            connection = DriverManager.getConnection(ur1, username, mypassword);
            PreparedStatement pst = null;
            ResultSet resultSet = null;
            String card = LoginJFrame.getUser().getCardNumber();
            String sql = "select * from demanddeposit where card = ?";
            pst = connection.prepareStatement(sql);
            pst.setString(1, card);
            resultSet = pst.executeQuery();
            while (resultSet.next()) {
                double money = resultSet.getDouble("money");
                String cno = resultSet.getNString("card");
                Date date = resultSet.getDate("depositDate");

                JLabel label0 = new JLabel(LoginJFrame.getUser().getName());
                JPanel jpanel = new JPanel();
                JLabel label1 = new JLabel(String.valueOf(money));
                JLabel label2 = new JLabel(cno);
                JLabel label3 = new JLabel(String.valueOf(date));
                double rate = new DemandRate().getDemandRate();
                JLabel label4 = new JLabel(String.valueOf(rate));

                Font font = new Font("宋体", Font.PLAIN, 20);

                label0.setFont(font);
                label1.setFont(font);
                label2.setFont(font);
                label3.setFont(font);
                label4.setFont(font);

                jpanel.add(label0);
                jpanel.add(label2);
                jpanel.add(label1);
                jpanel.add(label3);
                jpanel.add(label4);
                jFrame.add(jpanel);
            }
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    public void queryTimeRecords(TimeRecordsJFrame jFrame) throws Exception{
        User user = LoginJFrame.getUser();
        Statement statement = null;
        ResultSet result = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String ur1 = "jdbc:mysql://localhost:3306/bank";
            String username = "root";               //数据库用户名
            String mypassword = "20222746";          //数据库密码
            connection = DriverManager.getConnection(ur1, username, mypassword);
            PreparedStatement pst = null;
            ResultSet resultSet = null;
            String card = LoginJFrame.getUser().getCardNumber();
            String sql = "select * from timedeposit where card = ?";
            pst = connection.prepareStatement(sql);
            pst.setString(1, card);
            resultSet = pst.executeQuery();
            while (resultSet.next()) {
                double money = resultSet.getDouble("money");
                String cno = resultSet.getNString("card");
                Date date = resultSet.getDate("depositDate");
                String period = resultSet.getNString("period");

                JLabel label0 = new JLabel(LoginJFrame.getUser().getName());
                JPanel jpanel = new JPanel();
                JLabel label1 = new JLabel(String.valueOf(money));
                JLabel label2 = new JLabel(cno);
                JLabel label3 = new JLabel(String.valueOf(date));
                JLabel label4 = new JLabel(period);
                TimeRate rate = new TimeRate();
                int month = getMonth(period);
                JLabel label5 = new JLabel(String.valueOf(rate.getTimeDepositRate(month)));


                Font font = new Font("宋体", Font.PLAIN, 20);
                label0.setFont(font);
                label1.setFont(font);
                label2.setFont(font);
                label3.setFont(font);
                label4.setFont(font);
                label5.setFont(font);

                jpanel.add(label0);
                jpanel.add(label2);
                jpanel.add(label1);
                jpanel.add(label3);
                jpanel.add(label5);
                jpanel.add(label4);
                jFrame.add(jpanel);
            }
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void queryWithdrawalRecords(WithdrawalRecordsJFrame jFrame) throws Exception{
        User user = LoginJFrame.getUser();
        ResultSet result = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String ur1 = "jdbc:mysql://localhost:3306/bank";
            String username = "root";               //数据库用户名
            String mypassword = "20222746";          //数据库密码
            connection = DriverManager.getConnection(ur1, username, mypassword);
            PreparedStatement pst = null;
            ResultSet resultSet = null;
            String card = LoginJFrame.getUser().getCardNumber();
            System.out.println(card);
            String sql = "select * from withdrawal where cardNumber = ?";
            pst = connection.prepareStatement(sql);
            pst.setString(1, card);
            resultSet = pst.executeQuery();
            System.out.println("111111111");
            while (resultSet.next()) {
                System.out.println("2222222222");
                String cno = resultSet.getNString("cardNumber");
                System.out.println(cno);
                String type = resultSet.getNString("type");
                System.out.println(type);
                double money = resultSet.getDouble("money");
                System.out.println(money);
                double interest = resultSet.getDouble("Interest");
                System.out.println(interest);
                Date date = resultSet.getDate("Date");
                System.out.println(date);

                JPanel jpanel = new JPanel();
                JLabel label1 = new JLabel(cno);
                JLabel label2 = new JLabel(type);
                JLabel label3 = new JLabel(String.valueOf(money));
                JLabel label4 = new JLabel(String.valueOf(interest));
                JLabel label5 = new JLabel(String.valueOf(date));

                Font font = new Font("宋体", Font.PLAIN, 20);
                label1.setFont(font);
                label2.setFont(font);
                label3.setFont(font);
                label4.setFont(font);
                label5.setFont(font);

                jpanel.add(label1);
                jpanel.add(label2);
                jpanel.add(label3);
                jpanel.add(label4);
                jpanel.add(label5);
                jFrame.add(jpanel);
            }
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private void queryTransferRecords(TransferRecordsJFrame jFrame) throws Exception {
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String ur1 = "jdbc:mysql://localhost:3306/bank";
            String username = "root";               //数据库用户名
            String mypassword = "20222746";          //数据库密码
            connection = DriverManager.getConnection(ur1, username, mypassword);
            PreparedStatement pst = null;
            ResultSet resultSet = null;
            String card = LoginJFrame.getUser().getCardNumber();
            String sql = "select * from transfer where transferCardNumber = ?";
            pst = connection.prepareStatement(sql);
            pst.setString(1, card);
            resultSet = pst.executeQuery();
            while (resultSet.next()) {
                String outcard = resultSet.getNString("transferCardNumber");
                String outname = resultSet.getNString("transferName");
                String inname = resultSet.getNString("receiveName");
                double money = resultSet.getDouble("transferMoney");
                String incard = resultSet.getNString("receiveCardNumber");
//                System.out.println(outcard);
//                System.out.println(outname);
//                System.out.println(inname);
//                System.out.println(money);
//                System.out.println(incard);

                JPanel jpanel = new JPanel();

                JLabel label0 = new JLabel(outcard);
                JLabel label1 = new JLabel(outname);
                JLabel label2 = new JLabel(inname);
                JLabel label3 = new JLabel(String.valueOf(money));
                JLabel label4 = new JLabel(incard);


                Font font = new Font("宋体", Font.PLAIN, 20);

                label0.setFont(font);
                label1.setFont(font);
                label2.setFont(font);
                label3.setFont(font);
                label4.setFont(font);

                jpanel.add(label0);
                jpanel.add(label1);
                jpanel.add(label2);
                jpanel.add(label3);
                jpanel.add(label4);
                jFrame.add(jpanel);
            }
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }



    }

    public int getMonth(String s) {
        int a = 0, b = 0;
        if('0' <= s.charAt(0) && s.charAt(0) <= '9')
            a = s.charAt(0) - '0';
        if('0' <= s.charAt(1) && s.charAt(1) <= '9')
            b = s.charAt(1) - '0';
        return 10 * a + b;
    }
}
